package weixin02.service.impl;

import org.apache.commons.lang3.StringUtils;
import weixin02.entity.SysUser;
import weixin02.dao.SysUserDao;
import weixin02.service.SysUserService;
import org.springframework.stereotype.Service;
import weixin02.util.R;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * (SysUser)表服务实现类
 *
 * @author makejava
 * @since 2021-07-06 21:14:24
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserDao sysUserDao;


    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<SysUser> queryAllByLimit(int offset, int limit) {
        return this.sysUserDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public SysUser insert(SysUser sysUser) {
        this.sysUserDao.insert(sysUser);
        return sysUser;
    }

    @Override
    public SysUser update(SysUser sysUser) {
        return null;
    }


    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.sysUserDao.deleteById(id) > 0;
    }

    //登录
    @Override
    public R login(SysUser sysUser) {
        // 空值处理
        if(sysUser == null){
            return new R("9999","非法调用!");
        }
        if (StringUtils.isBlank(sysUser.getUsername())) {

            return new R("9999","账号不能为空!");
        }
        if (StringUtils.isBlank(sysUser.getPassword())) {
            return new R("9999","密码不能为空!");
        }

        // 验证账号
        SysUser dbUser = sysUserDao.queryByName(sysUser.getUsername());
        if (dbUser == null) {
            return new R("9999","该账号不存在!");
        }

        // 验证密码
        if (!dbUser.getPassword().equals(sysUser.getPassword())) {
            return new R("9999","密码错误!");
        }

        return new R("0","登录成功!");

    }


}
